package com.example.njut;

import android.Manifest;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

public class MainActivity extends AppCompatActivity {

    TextView welName;
    Button fl;
    private Button record;
    private Button chronometer;
    private Button alertDialog;
    private Button con;
    private Button bnote;
    private Button MyInfo;
    private Button rb;
    private Button game;
    private Button compass;
    private Button danmu;
    private Button weather;
    //private Button check;

    GestureLibrary gestureLibrary;

    //声明数组，用来保存所有需要动态开启的权限
    private static String[] PERMISSIONS_STORGE = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE
    };
    //权限的请求编码
    private static int REQUEST_PERMISSION_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

        welName = (TextView)findViewById(R.id.welcomeUserName);
        fl = (Button)findViewById(R.id.flashlight);
        record = (Button) findViewById(R.id.record);
        chronometer = (Button) findViewById(R.id.chronometer);
        alertDialog = (Button) findViewById(R.id.alertDialog);
        con = (Button) findViewById(R.id.con);
        bnote = (Button) findViewById(R.id.bnote);
        MyInfo = (Button) findViewById(R.id.MyInfo);
        rb = (Button) findViewById(R.id.rb);
        game = (Button) findViewById(R.id.game);
        compass = (Button) findViewById(R.id.compass);
        danmu = (Button) findViewById(R.id.danmu);
        weather = (Button) findViewById(R.id.weather);
        //check = (Button) findViewById(R.id.check);


        if(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP){
            if(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!=
                    PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(this,PERMISSIONS_STORGE,REQUEST_PERMISSION_CODE);
            }
        }

        //读取程序创建的手势库

        Intent intent = getIntent();
        final String name = intent.getStringExtra("uname");
        if(intent.getStringExtra("uname")!=null) {
            welName.setText("欢迎您，" + intent.getStringExtra("uname"));
        }

        fl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,FlashLightActivity.class);
                startActivity(intent);
            }
        });

        record.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,RecordActivity.class);
                startActivity(intent);
            }
        });
        chronometer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,TimeActivity.class);
                startActivity(intent);
            }
        });

        alertDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,AlertDialogActivity.class);
                startActivity(intent);
            }
        });
        con.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,ContentActivity.class);
                startActivity(intent);
            }
        });
        bnote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,NoteActivity.class);
                startActivity(intent);
            }
        });
        MyInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("uname",name);
                intent.setClass(MainActivity.this,MyInfoActivity.class);
                startActivity(intent);
            }
        });
        rb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,CountDownActivity.class);
                startActivity(intent);
            }
        });

        game.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                gestureLibrary = GestureLibraries.fromFile("/mnt/sdcard/zhanghan");
                if(gestureLibrary.load()) {
                    intent.setClass(MainActivity.this, CheckActivity.class);
                    startActivity(intent);
                }else{
                    intent.setClass(MainActivity.this, GestureActivity.class);
                    startActivity(intent);
                }
            }
        });

        /*check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,CheckActivity.class);
                startActivity(intent);
            }
        });*/
        compass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,CompassActivity.class);
                startActivity(intent);
            }
        });

        danmu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,DanmuActivity.class);
                startActivity(intent);
            }
        });

        weather.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,WeatherActivity.class);
                startActivity(intent);
            }
        });
    }

}
